import { defineStore } from "pinia";
import { MockGetDict } from "@/services/mock";

//数据字典
export const useDictStore = defineStore("dict", {
    state: () => ({
        // 40004: [], //航站楼
        data: {}
    }),
    //开启数据缓存
    persist: {
        enabled: true,
    },
    getters: {
        //获取字典项
        getDict: (state) => {
            return (key) => {
                state.getKinds(key);
                if (!key || !state.data[key] || !state.data[key].length) return [];
                return state.data[key];
            };
        },

        //根据字典项key和code获取对应的值
        getDictVal(state) {
            return (key, code) => {
                if (!state.data[key] || !state.data[key].length) return "";
                let _data = state.data[key].find((v) => v.value == code);
                if (!_data) return "";
                return _data.label;
            };
        },
    },
    actions: {
        async getKinds(key) {
            if (!key) return;
            if (this.data[key] && this.data[key].length) return;
            let res = await MockGetDict({ kind: key });
            if (res.code === 200) {
                this.data[key] = res.data;
            }
        },
    },
});

export default useDictStore;
